package com.fivefu.core.skyeye.event.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.skyeye.event.entity.DbAiEventImage;
import com.fivefu.core.skyeye.event.mapper.DbAiEventImageMapper;
import com.fivefu.core.skyeye.event.services.DbAiEventImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.skyeye.event.vo.EventImageBean;
import com.fivefu.core.skyeye.event.vo.EventImageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 案卷图片表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-24 11:57:55
 */
@Service
public class DbAiEventImageServiceImpl extends ServiceImpl<DbAiEventImageMapper, DbAiEventImage> implements DbAiEventImageService {

    /**
     * 通过编号查询事件图片
     * @param mimirId 识别图片编号
     * @return
     */
    public DbAiEventImage queryEventImageByMimirImageId(Long mimirId){
        QueryWrapper<DbAiEventImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("mimir_id",mimirId);
        DbAiEventImage image = baseMapper.selectOne(wrapper);
        return image;
    }

    /**
     * 编辑事件图片
     * @param eventId 事件编号
     * @param image 图片
     * @return
     */
    @Override
    public Long editEventImage(Long eventId, EventImageBean image){
        LocalDateTime time = LocalDateTime.now();

        LocalDateTime interceptTime = DateUtils.parse(image.getImageTime(), "yyyyMMddHHmmss");
        String imageUrl = image.getImagePath();
        String suffix = imageUrl.substring(imageUrl.lastIndexOf(".")+1);

        DbAiEventImage img = new DbAiEventImage();
        img.setEventId(eventId);
        img.setInterceptTime(interceptTime);
        img.setImageType(suffix);
        img.setImageUrl(imageUrl);
        img.setMimirId(image.getImageSlicesId());
        img.setUpdatedTime(time);
        DbAiEventImage eventImage = queryEventImageByMimirImageId(image.getImageSlicesId());
        if (eventImage != null){
            img.setId(eventImage.getId());
            baseMapper.updateById(img);
        }else{
            img.setCreatedTime(time);
            img.setIsDelete(0);
            baseMapper.insert(img);
        }
        return img.getId();
    }

    /**
     * 编辑事件图片
     * @param eventId 事件编号
     * @param images 图片集合
     * @return
     */
    @Override
    public void editEventImage(Long eventId, List<EventImageBean> images){
        images.forEach(image -> {
            editEventImage(eventId, image);
        });
    }

    /**
     * 查看案卷图片详情
     * @param eventid  事件编号
     * @return
     */
    @Override
    public List<EventImageVo> getFileInfo(Long eventid) {
        List<EventImageVo> resultList = new ArrayList<>();
        List<DbAiEventImage> aiEventImageList = getAiEventImageByEventId(eventid, null);
        if (null != aiEventImageList && aiEventImageList.size() > 0) {
            aiEventImageList.forEach(dbAiEventImage -> {
                EventImageVo vo = formatDbAiEventImage(dbAiEventImage);
                resultList.add(vo);
            });
        }
        return resultList;
    }

    /**
     * 通过事件编号和图片编号集合查询相关案卷图片数据集
     * @param eventid  事件编号
     * @param imageIDs 图片编号集合
     * @return
     */
    @Override
    public List<DbAiEventImage> getAiEventImageByEventId(Long eventid, List<Long> imageIDs) {
        QueryWrapper<DbAiEventImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (eventid != null) {
            wrapper.eq("event_id", eventid);
        }
        if (imageIDs != null && !imageIDs.isEmpty()){
            wrapper.in("id",imageIDs);
        }
        List<DbAiEventImage> aiEventImageList = baseMapper.selectList(wrapper);
        return aiEventImageList;
    }

    /**
     * 格式化事件图片信息
     * @param dbAiEventImage 图片信息
     * @return
     */
    private EventImageVo formatDbAiEventImage(DbAiEventImage dbAiEventImage) {
        EventImageVo eventImageVo = new EventImageVo();
        BeanUtils.copyProperties(dbAiEventImage,eventImageVo);
        //创建时间
        if (null != eventImageVo.getCreatedTime()) {
            String time = DateUtils.format(eventImageVo.getCreatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            eventImageVo.setCreatedTimeStr(time);
        }
        //更新时间
        if (null != eventImageVo.getUpdatedTime()) {
            String time = DateUtils.format(eventImageVo.getUpdatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            eventImageVo.setUpdatedTimeStr(time);
        }
        //截取时间
        if (null != eventImageVo.getInterceptTime()) {
            String time = DateUtils.format(eventImageVo.getInterceptTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            eventImageVo.setInterceptTimeStr(time);
        }
        return eventImageVo;
    }
}
